package com.example.imitation_webchat_demo.adapter;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Filter;
import android.widget.Filterable;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.imitation_webchat_demo.R;
import com.example.imitation_webchat_demo.entity.MessageItem;
import com.example.imitation_webchat_demo.model.BadgeView;

// 消息页面所对应的适配器类MessageAdapter，实现Filterable接口
public class MessageAdapter extends BaseAdapter implements Filterable {
	private Context context;
	private List<MessageItem> list;
	private MyFilter myFilter;
	private List<MessageItem> copyList;
	
	public MessageAdapter(Context context, List<MessageItem> list) {
		this.context = context;
		this.list = list;
		copyList = list;
	}

	@Override
	public int getCount() {
		return list.size();
	}

	@Override
	public Object getItem(int position) {
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder viewHolder;
		MessageItem item = list.get(position);
		if (convertView == null) {
			convertView = LayoutInflater.from(context).inflate(
					R.layout.message_item, null);
			ImageView iconImage = (ImageView) convertView
					.findViewById(R.id.message_item_image);
			TextView titleView = (TextView) convertView
					.findViewById(R.id.message_item_title);
			TextView messageView = (TextView) convertView
					.findViewById(R.id.message_item_message);
			TextView timeView = (TextView) convertView
					.findViewById(R.id.message_item_time);
			BadgeView badgeView = new BadgeView(context, iconImage);
			viewHolder = new ViewHolder(iconImage, titleView, messageView,
					timeView, badgeView);
			convertView.setTag(viewHolder);
		} else {
			viewHolder = (ViewHolder) convertView.getTag();
		}
		viewHolder.iconImage.setImageResource(item.getImageId());
		viewHolder.badgeView.setIntegerText(item.getNumber());
		viewHolder.titleView.setText(item.getTitle());
		viewHolder.messageView.setText(item.getMessage());
		viewHolder.timeView.setText(item.getTime());
		return convertView;
	}

	private class ViewHolder {
		ImageView iconImage;
		TextView titleView;
		TextView messageView;
		TextView timeView;
		BadgeView badgeView;

		public ViewHolder(ImageView iconImage, TextView titleView,
				TextView messageView, TextView timeView, BadgeView badgeView) {
			this.iconImage = iconImage;
			this.titleView = titleView;
			this.messageView = messageView;
			this.timeView = timeView;
			this.badgeView = badgeView;
		}
	}

	@Override
	public Filter getFilter() {
		if (myFilter == null) {
			myFilter = new MyFilter();
		}
		return myFilter;
	}

	// 自定义过滤器
	private class MyFilter extends Filter {

		@Override
		protected FilterResults performFiltering(CharSequence sequence) {
			FilterResults results = new FilterResults();
			String filterString = sequence.toString();
			List<MessageItem> newValues = new ArrayList<MessageItem>();

			if (filterString == null || filterString.length() == 0) {
				results.values = copyList;
				results.count = copyList.size();
			} else {
				for (MessageItem item : copyList) {
					String title = item.getTitle();
					if (title.contains(filterString)) {
						newValues.add(item);
					}
				}
				results.values = newValues;
				results.count = newValues.size();
			}
			return results;
		}

		@Override
		protected void publishResults(CharSequence constraint,
				FilterResults results) {
			list = (List<MessageItem>) results.values;
			notifyDataSetChanged();
		}
	}
}